package com.mjlf.framework.mvc.util;

import com.mjlf.framework.annotation.mvc.Controller;
import com.mjlf.framework.annotation.mvc.RequestMapping;
import com.mjlf.framework.mvc.eneity.RequestEntity;
import com.mjlf.framework.mvc.eneity.RequestMethod;
import com.mjlf.framework.util.BeanManager;
import com.mjlf.framework.util.ClassManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 调用这个方法前必须先实例化ioc
 */
public final class RequestManager {
    public final static Map<RequestEntity, RequestMethod> REQ_METHOD = new HashMap<>();
    private final static Logger logger = LoggerFactory.getLogger(RequestManager.class);

    public static void init() {
        Set<Class<?>> classes = ClassManager.getClassSet(Controller.class);
        if (classes != null && !classes.isEmpty()) {
            try {
                for (Class<?> cls : classes) {

                    if (cls.isAnnotationPresent(Controller.class)) {
                        Object object = BeanManager.getBean(cls);
                        String reqPathRoot = "";
                        String reqMethod = "GET";
                        //<editor-fold desc="判断类上是添加@RequestMapping注解">
                        if(cls.isAnnotationPresent(RequestMapping.class)){
                            RequestMapping requestMapping = cls.getAnnotation(RequestMapping.class);
                            reqPathRoot = requestMapping.value();
                            reqMethod = requestMapping.method();
                        }
                        //</editor-fold>
                        Method[] methods = cls.getDeclaredMethods();
                        for(Method method : methods){
                            if(method.isAnnotationPresent(RequestMapping.class)){
                                RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);
                                String reqPath = reqPathRoot + requestMapping.value();
                                reqMethod = requestMapping.method();

                                RequestEntity requestEntity = new RequestEntity(reqMethod, reqPath);
                                RequestMethod requestMethod = new RequestMethod(object, method);

                                REQ_METHOD.put(requestEntity, requestMethod);
                            }
                        }
                    }
                }
            }catch (Exception e){
                logger.error("请求路径存在配置存在错误", e);
                throw new RuntimeException(e);
            }
        }
    }

    public static RequestMethod getReqMethod(String reqMethod, String reqPath) throws Exception {
        RequestEntity requestEntity = new RequestEntity(reqMethod, reqPath);
        if(REQ_METHOD.containsKey(requestEntity)){
            return REQ_METHOD.get(requestEntity);
        }else {
            return null;
        }
    }

    public static RequestMethod getReqMethod(RequestEntity requestEntity) throws Exception {
        if(REQ_METHOD.containsKey(requestEntity)){
            return REQ_METHOD.get(requestEntity);
        }else {
            return null;
        }
    }
}
